package cn.ds.assets.app;

import cn.ds.assets.framework.test.core.ut.BaseDbUnitTest;
import cn.ds.assets.module.infra.framework.codegen.config.CodegenProperties;
import cn.ds.assets.module.infra.service.codegen.CodegenServiceImpl;
import cn.ds.assets.module.infra.service.codegen.inner.CodegenBuilder;
import cn.ds.assets.module.infra.service.codegen.inner.CodegenEngine;
import cn.ds.assets.module.infra.service.db.DataSourceConfigServiceImpl;
import cn.ds.assets.module.infra.service.db.DatabaseTableServiceImpl;
import cn.ds.assets.module.system.api.user.AdminUserApiImpl;
import cn.ds.assets.module.system.mq.producer.dept.DeptProducer;
import cn.ds.assets.module.system.service.dept.DeptServiceImpl;
import cn.ds.assets.module.system.service.user.AdminUserServiceImpl;
import cn.hutool.core.io.file.FileWriter;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * {@link SingleCodeServiceApplication} 创建非公共代码到桌面
 *
 * @author 芋道源码
 */
@Import({
        CodegenTool.class,CodegenEngine.class, CodegenProperties.class,
        CodegenServiceImpl.class,
        DatabaseTableServiceImpl.class,
        DataSourceConfigServiceImpl.class,
        DynamicDataSourceProperties.class,
        AdminUserApiImpl.class,
        AdminUserServiceImpl.class,
        DeptServiceImpl.class,
        DeptProducer.class,
        CodegenBuilder.class
})
@Slf4j
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public class SingleCodeServiceApplication extends BaseDbUnitTest {

    @Autowired
    CodegenTool codegenTool;

    @Test
    public void testCreateTestDemo_success() {
        String tableName = "system_role";

        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        // s为当前用户桌面所在路径
        String projectPath = desktopDir.getAbsolutePath()+"/Codegen/";
        System.out.println(projectPath);
        //把所有表加入到同步列表中
        List<Long> tableIds = codegenTool.createCodegenAll(tableName);
        //生成文件内容
        Map<String, String> codes = codegenTool.generationCodes(tableIds,tableName.substring(0,tableName.indexOf("_")));
        //删除原来的文件
        File file = new File(projectPath);
        if (file.exists()) {
            try {
                FileUtils.forceDelete(file);
                log.info("原生成的代码已删除");
            } catch (IOException e) {
                log.info("原生成的代码未删除");
                e.printStackTrace();
            }
        }
        //覆盖文件
        codes.forEach((key,value)->{
            String path = projectPath+key;
            FileWriter.create(new File(path));
            FileWriter writer = new FileWriter(path);
            writer.write(value);
        });
        System.out.println("自动生成代码成功,目录地址为:"+projectPath);
    }
}
